package com.dmm.vod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmm.vo.vod.ChapterVo;
import com.dmm.vo.vod.VideoVo;
import com.dmm.vod.entity.Chapter;
import com.dmm.vod.entity.Video;
import com.dmm.vod.mapper.ChapterMapper;
import com.dmm.vod.service.ChapterService;
import com.dmm.vod.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dmm
 * @since 2022-09-07
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    VideoService videoService;



    //获取章节小结列表
    @Override
    public List<ChapterVo> getNestedTreeList(Long courseId) {


        List<ChapterVo> chapterVoList = new ArrayList<>();
        QueryWrapper<Chapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<Chapter> chapters = baseMapper.selectList(wrapperChapter);

        QueryWrapper<Video> wrapperVido=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<Video> list = videoService.list(wrapperVido);
        for (Chapter chapter :chapters){
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);

            List<VideoVo> videoVoList = new ArrayList<>();
            for(Video video:list){
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        }


        return chapterVoList;
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(Long id) {
        QueryWrapper<Chapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        baseMapper.delete(queryWrapper);
    }
}
